
#include <Windows.h>
#include <time.h>
#define  random(x) (rand()%x)

using namespace std;
#include <iostream>


// 前向声明
class Level;

class GameAccount
{
public:
	GameAccount();
	GameAccount(string iName);
	string getName();
	void win();
	void lose();
	void playCard();
	void setLevel(Level*);
	int getScore();
	void setScore(int);

private:
	Level* level;
	int score;
	string name;
};

// 抽象状态类
class Level
{
public :
	Level() = default;
	// 声明方法
	void playCard();
	void play();
	virtual void doubleScore() = 0;
	virtual void changeCards() = 0;
	virtual void peekCards() = 0;
	// 升级
	virtual void upgradeLevel() = 0;
	GameAccount* getGameAccount();
	void setGameAccount(GameAccount* iGameAccount);
private:
	GameAccount* gameAccount;
};


// 具体状态类
class Primary :public Level
{
public:
	Primary();
	Primary(Level* level);
	Primary(GameAccount* ga);
	void doubleScore();
	void changeCards();
	void peekCards();
	// 升级
	void upgradeLevel();
};

// 具体状态类
class Secondary :public Level
{
public:
	Secondary();
	Secondary(Level* level);
	void doubleScore();
	void changeCards();
	void peekCards();
	// 升级
	void upgradeLevel();
};

// 具体状态类
class Professional :public Level
{
public:
	Professional();
	Professional(Level* level);
	void doubleScore();
	void changeCards();
	void peekCards();
	// 升级
	void upgradeLevel();
};

// 具体状态类
class Final :public Level
{
public:
	Final();
	Final(Level* level);
	void doubleScore();
	void changeCards();
	void peekCards();
	// 升级
	void upgradeLevel();
};

GameAccount::GameAccount(){
	printf("创立游戏角色，积分：100，级别：PRIMARY\n");
	score = 100;
	name = "none";
	setLevel(new Primary(this));
}

GameAccount::GameAccount(string iName){
	printf("创立游戏角色，积分：100，级别：PRIMARY\n");
	score = 100;
	name = iName;
	setLevel(new Primary(this));
}

void GameAccount::setLevel(Level* iLevel){
	this->level = iLevel;
}

string GameAccount::getName(){
	return name;
}

void GameAccount::playCard(){
	this->level->playCard();

	Sleep(100);
	srand((int)time(0));
	int res = random(200);
	Sleep(1000);
	cout << "res : " << res << endl;
	if (res % 2 == 0){
		this->win();
	}
	else{
		this->lose();
	}

	this->level->upgradeLevel();
}

void GameAccount::win(){
	if (this->getScore() < 200){
		setScore(getScore() + 50);
	}
	else{
		setScore(getScore() + 100);
	}
	printf("\n\t胜利，最新积分为 %d\n", score);
}

void GameAccount::lose(){
	setScore(getScore() + 30);
	printf("\n\t输牌，最新积分为 %d\n", score);
}

int GameAccount::getScore(){
	return this->score;
}

void GameAccount::setScore(int iScore){
	this->score = iScore;
}

void Level::playCard(){
	this->play();
	this->doubleScore();
	this->changeCards();
	this->peekCards();
}

void Level::play(){
	printf("\t使用基本技能,");
}

void Level::setGameAccount(GameAccount* iGameAccount){
	this->gameAccount = iGameAccount;
}

GameAccount* Level::getGameAccount(){
	return gameAccount;
}

/****************	具体状态类 Primary	****************/

Primary::Primary(GameAccount* iGameAccount){
	this->setGameAccount(iGameAccount);
}

Primary::Primary(Level* level){
	getGameAccount()->setLevel(level);
}

void Primary::doubleScore(){
	return;
}

void Primary::changeCards(){
	return;
}

void Primary::peekCards(){
	return;
}

void Primary::upgradeLevel(){
	if (this->getGameAccount()->getScore() > 150){
		this->getGameAccount()->setLevel(new Secondary(this));
		printf("\t升级！ 级别：SECONDARY\n\n");
	}
	else{
		printf("\n");
	}
}

/****************	具体状态类 Secondary	****************/

Secondary::Secondary(Level* level){
	this->setGameAccount(level->getGameAccount());
	getGameAccount()->setLevel(level);
}

void Secondary::doubleScore(){
	printf("使用胜利双倍积分技能");
}

void Secondary::changeCards(){
	return;
}

void Secondary::peekCards(){
	return;
}

void Secondary::upgradeLevel(){
	if (this->getGameAccount()->getScore() < 150){
		this->getGameAccount()->setLevel(new Primary(this));
		printf("\t降级！ 级别：PRIMARY\n\n");
	}
	else if (this->getGameAccount()->getScore() > 200){
		this->getGameAccount()->setLevel(new Professional(this));
		printf("\t升级！ 级别：PROFESSIONAL\n\n");
	}
}

/****************	具体状态类 Professional	****************/

Professional::Professional(Level* level){
	this->setGameAccount(level->getGameAccount());
	getGameAccount()->setLevel(level);
}

void Professional::doubleScore(){
	printf("使用胜利双倍积分技能,");
}

void Professional::changeCards(){
	printf("使用换牌技能");
}

void Professional::peekCards(){
	return;
}

void Professional::upgradeLevel(){
	if (this->getGameAccount()->getScore() < 200){
		this->getGameAccount()->setLevel(new Secondary(this));
		printf("\t降级！ 级别：SECONDARY\n\n");
	}
	else if (this->getGameAccount()->getScore() > 250){
		this->getGameAccount()->setLevel(new Final(this));
		printf("\t升级！ 级别：FINAL\n\n");
	}
}

/****************	具体状态类 Final	****************/

Final::Final(Level* level){
	this->setGameAccount(level->getGameAccount());
	getGameAccount()->setLevel(level);
}

void Final::doubleScore(){
	printf("使用胜利双倍积分技能,");
}

void Final::changeCards(){
	printf("使用换牌技能,");
}

void Final::peekCards(){
	printf("使用偷看卡牌技能");
}

void Final::upgradeLevel(){
	if (this->getGameAccount()->getScore() < 250){
		this->getGameAccount()->setLevel(new Professional(this));
		printf("\t降级！ 级别：PROFESSIONAL\n\n");
	}
	else{
		printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
	}
}


int main()
{
	GameAccount *jungle = new GameAccount("Jungle");

	for (int i = 0; i < 5; i++){
		printf("第%d局：\n", i + 1);
		jungle->playCard();
	}

	printf("\n\n");
	system("pause");
	return 0;
}

